<?php
	require '../biz/backend.php';

	class action extends backend
	{
		var $get_privilege_id = 87;

		function do_get()
		{
			$template = load( 'template', array( 'domain'=>'tpl.admin' ) );

			$tpl = load( 'biz.tpl' );
			$tmp = $tpl->get( $this->input );
			if ( ! empty( $tmp ) )
			{
				$template->assign( 'refer_tpl_id', $tmp['refer_tpl_id'] );
				$tpl_type = $tmp['type'];

				//模板类型
				$argument = load( 'biz.argument' );
				$template->assign( 'tpl_type_cname', $argument->tpl_type_cname[$tpl_type] );

				//模板源文件
				$tpl_file = config( 'dir.tpl.root' ) . $tmp['path'];
				if ( is_file( $tpl_file ) && is_readable( $tpl_file ) )
				{
					$file_content = file_get_contents( $tpl_file );
					$template->assign( 'file_content', $file_content );
				}
				else
				{
					$this->prompt( '模板文件不存在', array( array( 'url' => 'list_tpl.php', 'name' => '返回列表' ) ) );
					return false;
				}

				//获取所有标签类列表
				$tag = load( 'biz.tag' );
				$taglist = $tag->all( $this->input );
				$template->assign( 'taglist', $taglist['data'] );

				//获取当前模板定义的所有标签类列表
				$tpltaglist = $tag->get_tag_define( $this->input['id'] );
				$template->assign( 'tpltaglist', $tpltaglist );


				//当前栏目的名称
				$cate = load( 'biz.cate' );
				if ( $tmp['cate_id'] != 0 )
				{
					$cate_data = $cate->get( array( 'id'=>$tmp['cate_id'] ) );
				}
				else
				{
					$cate_data['cname'] = '整站通用栏目';
				}
				$template->assign( 'cate_name', $cate_data['cname'] );

				//获取栏目列表
				$cate = load( 'biz.cate' );
				$result = $cate->getcatealllist();
				$template->assign( 'cate_data', $result );

				$template->appoint( $this->input );
				$template->assign( 'reference', isset( $this->input['reference'] ) ? urldecode( $this->input['reference'] ) : 'list_tpl.php' );

				echo $template->parse( 'edit_tpl.php' );
			}
			else
			{
				$this->prompt( '模板文件不存在', array( array( 'url' => 'list_tpl.php', 'name' => '返回列表' ) ) );
			}
		}


		/**
		 * description
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function do_post()
		{
			$url = isset( $this->input['reference'] ) ? $this->input['reference'] : config( 'domain.admin' ) . 'list_tpl.php';
			unset( $this->input['reference'] );

			$tpl = load( 'biz.tpl' );
			$result = $tpl->set( $this->input );

			if ( $result['success'] )
			{
				$this->prompt( '编辑成功', array( array( 'url' => config( 'domain.admin' ) . 'edit_tpl.php?id=' . $this->input['id'], 'name' => '继续编辑' ), array( 'url' => 'list_tpl.php', 'name' => '返回列表' ) ) );
			}
			else
			{
				$this->prompt( '编辑失败', $this->transform( $result['message'], config( 'server.HTTP_REFERER' ) ) );
			}
		}

		/**
		 * 覆盖父类方法，避免html内容无法提交问题
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return array
		 */
		function clean( & $array )
		{
			foreach( $array as $key	=> $value ) $array[$key] = is_array( $array[$key] ) ? $this->clean( $value ) : $this->format( $value );
			return $array;
		}

		//格式化传入参数
		function format( $value )
		{
			if ( get_magic_quotes_gpc() )
			{
				$value = stripslashes( $value );
			}

			return trim( $value ) ;
		}

	}

	$action = new action();
	$action->run();
?>